﻿using SuYuanSystem.Applications;
using MediatR;
using SuYuanSystem.Applications;
using SuYuanSystem.Domain;
using SuYuanSystem.Repositories;
using SuYuanSystem.WebApi.DTOs;
using SuYuanSystem.WebApi.MedialRs.Commands.TracebilitysDetile;

namespace SuYuanSystem.WebApi.MedialRs.CommandHandlers.TracebilitysDetile
{
    public class TracebilitysDetileCommandsHandlers : IRequestHandler<TracebilitysDetileCommands, ApiResult<TracebilityDto>>
    {
        private readonly IRepository<Tracebility> repository;
        private readonly IRepository<Shopp> shoppRepository;

        public TracebilitysDetileCommandsHandlers(IRepository<Tracebility> repository, IRepository<Shopp> ShoppRepository)
        {
            this.repository = repository;
            shoppRepository = ShoppRepository;
        }
        public Task<ApiResult<TracebilityDto>> Handle(TracebilitysDetileCommands request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<TracebilityDto> result = new ApiResult<TracebilityDto>();
                result.statusCode = StatusCode.Success;
                result.Message = "查询成功";
                var list = from a in repository.GetAll().Result
                           join b in shoppRepository.GetAll().Result on a.ShoppId equals b.ShoppId
                           where a.IsDelete == false
                           select new TracebilityDto
                           {
                               Id = a.Id,
                               TraceabilityId = a.TraceabilityId,
                               ShoppId = a.ShoppId,
                               ShoppName = b.ShoppName,
                               ProBatch = a.ProBatch,
                               ShelfLife = a.ShelfLife,
                               ProLicense = a.ProLicense,
                               Manufacturers = a.Manufacturers,
                               CoverPic = a.CoverPic,
                               IsDelete = a.IsDelete
                           };
                var entity = list.Where(x => x.TraceabilityId == request.TraceabilityId);
                result.Data = entity.FirstOrDefault();
                return Task.FromResult(result);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
